import request from '@/utils/request'

const baseURL = '/indicator'

// 新增或修改指标信息
export const saveOrUpdate = (data = {}) => {
  return request({
    url: `${baseURL}/saveOrUpdate`,
    method: 'post',
    data: data,
  })
}

// 通过点位代码查询指标列表
export const selectIndicatorList = (pointCode = []) => {
  return request({
    url: `${baseURL}/selectIndicatorList`,
    method: 'post',
    data: pointCode
  })
}

// 通过指标编码查询该指标下的数据列表
export const selectTagValueList = (data = {}) => {
  return request({
    url: `${baseURL}/selectTagValueList`,
    method: 'post',
    data
  })
}

// 根据ID查询指标信息
export const loadIndicator = (id) => {
  return request({
    url: `${baseURL}/loadIndicator`,
    params: {
      id: id,
    }
  })
}

// 根据ID删除指标信息
export const del = (id) => {
  return request({
    url: `${baseURL}/delete`,
    params: {
      id: id,
    }
  })
}

// 通过指标编码查询该指标下每15分钟的数据
export const select15MinutesTagValueList = (data = {}) => {
  return request({
    url: `${baseURL}/select15MinutesTagValueList`,
    method: 'post',
    data
  })
}

// 查询现有的测点代码列表
export const selectTagCodeList = () => {
  return request({
    url: `${baseURL}/selectTagCodeList`
  })
}

/**
 * 批量修改指标层级关系
 * @param pointCode 节点代码
 * @param list 指标ID集合
 * @returns {*}
 */
export function batchUpdateHierarchy (pointCode, list) {
  return request({
    url: `${baseURL}/batchUpdateHierarchy`,
    method: 'post',
    data: {
      pointCode,
      list
    }
  })
}

/**
 * 释放车间
 */
export function batchUnbundle (ids = []) {
  return request({
    url: `${baseURL}/batchUnbundle`,
    method: 'post',
    data: ids
  })
}

/**
 * 分页查询指标列表
 * @param pageNumber
 * @param pageSize
 */
export function selectIndicatorPageList (pageNumber = 1, pageSize = 20) {
  return request({
    url: `${baseURL}/selectIndicatorPageList`,
    method: 'post',
    data: {
      pageNumber,
      pageSize,
    }
  })
}

// 上传指标信息
export function importIndicatorList (file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: `${baseURL}/importIndicatorList`,
    method: 'post',
    data: formData
  })
}

// 下载指标模板
export function exportTemplateList () {
  return request({
    url: `${baseURL}/exportTemplateList`,
    responseType: 'blob'
  }).then(res => {
    console.log(res)
    const { data, headers } = res
    const fileName = (headers['content-disposition'] || '').replace(/\w+;filename=(.*)/, '$1')
    const blob = new Blob([data], { type: headers['content-type'] })
    const dom = document.createElement('a')
    const url = window.URL.createObjectURL(blob)
    dom.href = url
    dom.download = decodeURI(fileName)
    dom.style.display = 'none'
    document.body.appendChild(dom)
    dom.click()
    dom.parentNode.removeChild(dom)
    window.URL.revokeObjectURL(url)
  })
}
